package ldh.im.fxbase.richtext.item;

import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import ldh.im.fxbase.controller.ImagePageController;
import ldh.im.fxbase.richtext.TextItem;
import ldh.im.fxbase.richtext.TextItemType;
import ldh.im.fxbase.util.IconUtil;
import ldh.im.fxbase.util.RegionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageTextItem extends TextItem<ImageTextItem> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImageTextItem.class);

    private String imgUri;
    private boolean isErrorImage = false;

    public ImageTextItem(String imgUri) {
        this.imgUri = imgUri;
        if (imgUri == null) {
            this.imgUri = this.getClass().getResource("/img/error_picture.png").toExternalForm();
            isErrorImage = true;
        }
    }

    public Node createNode() {
        StackPane stackPane = new StackPane();
        stackPane.getStyleClass().add("image-cell");
        if (isErrorImage) {
            Label label = new Label();
            label.setGraphic(IconUtil.createIcon("fa-close", 28, Color.RED));
            label.setTooltip(new Tooltip("图片有误"));
            stackPane.getChildren().add(label);
            return stackPane;
        }
        ImageView imageView = new ImageView();

        imageView.setImage(new Image(imgUri));
        imageView.setPreserveRatio(true);
        if (imageView.getImage().getWidth() > imageView.getImage().getHeight()) {
            if (imageView.getImage().getWidth() > 300) {
                imageView.setFitWidth(300);
            }
            if (imageView.getImage().getHeight() > 80) {
                imageView.setFitHeight(80);
            }
        } else {
            if (imageView.getImage().getHeight() > 120) {
                imageView.setFitHeight(120);
            }
        }

        stackPane.setOnMouseClicked(e->{
            if (e.getClickCount() != 2) return;
            Stage stage = new Stage();
            RegionUtil.FxmlBean<VBox, ImagePageController> node = RegionUtil.loadFxmlBean(this.getClass().getResource("/fxmls/ImagePage.fxml"));
            node.getController().setImage(new Image(imgUri));
            Scene scene = new Scene(node.getNode());
            scene.getStylesheets().add(ImageTextItem.class.getResource("/csses/imagePane.css").toExternalForm());
            stage.setScene(scene);
            stage.show();
        });

        stackPane.getChildren().setAll(imageView);
        return stackPane;
    }

    public TextItemType getTextItemType() {
        return TextItemType.image;
    }

    public String getStyle() {
        return "image";
    }

    public String getImgUri() {
        return imgUri;
    }

    @Override
    public boolean isNull() {
        if (imgUri == null || imgUri.trim().equals("")) {
            return true;
        }
        return false;
    }

    @Override
    public String getData() {
        return getImgUri();
    }

}
